package com.smartmedical.auth.doctor.service;

import com.smartmedical.auth.doctor.form.PatientUserBody;
import com.smartmedical.auth.doctor.util.RSAUtils;
import com.smartmedical.auth.doctor.util.RsaKey;
import com.smartmedical.common.core.domain.R;

import com.smartmedical.common.core.exception.ServiceException;
import com.smartmedical.common.core.utils.StringUtils;
import com.smartmedical.system.api.RemotePatientUserService;
import com.smartmedical.system.api.domain.PatientUsers;
import com.smartmedical.system.api.model.PatientUserJwt;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @BelongsProject: smart-medical
 * @BelongsPackage: com.smartmedical.auth.service
 * @ClassName: PatientUserService
 * @Author: zyn
 * @CreateTime: 2024-01-08  18:15
 * @Description: TODO
 */
@Component
@Log4j2
public class PatientUserService {

    @Autowired
    private RemotePatientUserService remotePatientUserService;
    @Autowired
    private SysRecordLogService sysRoleService;

    @Autowired
    private SysRecordLogService recordLogService;

    @Autowired
    private SysPasswordService passwordService;


    public PatientUserBody login(String email, String pwd) {

        if (StringUtils.isAnyBlank(email, pwd)) {
            throw new RuntimeException("email or pwd is null");
        }
        R<PatientUserJwt> userResult = remotePatientUserService.getDoctorInfo(email);

        if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
        {
            throw new RuntimeException("登录邮箱：" + email + " 不存在");
        }

        PatientUserJwt data = userResult.getData();

        try {
            String password = RSAUtils.rsaDecrypt(pwd, RsaKey.PRIVATE_KEY);

            if (!data.getPatientUsers().getPwd().equals(password)) {
                throw new RuntimeException("密码错误,请重新输入");
            }

            PatientUsers patientUsers = data.getPatientUsers();

            //赋值
            PatientUserBody doctorEnity = createDoctorEnity(patientUsers);
            return doctorEnity;


        }catch (Exception e) {
            throw new ServiceException("公钥或私钥错误");// 业务异常
        }


    }

    private PatientUserBody createDoctorEnity(PatientUsers patientUsers) {
        PatientUserBody patientUserBody = new PatientUserBody();
        patientUserBody.setId(patientUsers.getId());
        patientUserBody.setPhone(patientUsers.getPhone());
        patientUserBody.setPwd(patientUsers.getPwd());
        patientUserBody.setEmail(patientUsers.getEmail());
        patientUserBody.setNickName(patientUsers.getNickName());
        patientUserBody.setUserName(patientUsers.getUserName());
        patientUserBody.setHeadPic(patientUsers.getHeadPic());
        patientUserBody.setSex(patientUsers.getSex());
        patientUserBody.setAge(patientUsers.getAge());
        patientUserBody.setHeight(patientUsers.getHeight());
        patientUserBody.setUpdateTime(patientUsers.getUpdateTime());
        patientUserBody.setUpdateTime(patientUsers.getUpdateTime());
        patientUserBody.setWeight(patientUsers.getWeight());
        patientUserBody.setInvitationCode(patientUsers.getInvitationCode());



        return patientUserBody;
    }
}
